package com.hdjk.view.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.callback.ListDialogCallBackListener;
import com.lst.projectlib.entity.BaseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/17.
 */

public class ListDialog extends RelativeLayout {

    private LinearLayout lin_dialog;
    private TextView dialog_content;
    // 信息列表提示框
    private AlertDialog alertDialog;
    private AlertDialog.Builder alertBuilder;
    private ListDialogCallBackListener listDialogCallBackListener;
    private String[] items;
    private String title;
    private int select = -1;
    private boolean flag = true;

    private Context context;

    public ListDialog(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public ListDialog(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    public void initTitle(String title) {
        this.title = title;
        setTitle();
    }

    public String getTitle(){
        return this.title;
    }

    public void initData(String title, List<BaseEntity> baseEntities, ListDialogCallBackListener listener) {
        this.listDialogCallBackListener = listener;

        // 设置标题
        initTitle(title);
        // 设置数据源
        initData(baseEntities);
        // 设置列表弹框
        setItems();
    }

    public void setEnabled(boolean flag) {
        this.flag = flag;
        if (!flag) {
            lin_dialog.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_btn_setting_unselect));
        }
    }

    public void notifyDataSetChanged(List<BaseEntity> baseEntities) {
        select = -1;

        setTitle();

        initData(baseEntities);

        alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialoginterface, int index) {
                dialog_content.setText(items[index]);
                dialog_content.setTextColor(context.getResources().getColor(R.color.text_black_202020));
                alertDialog.dismiss();
                changeIndex(index);
            }
        });
        alertDialog = alertBuilder.create();
    }

    public int getItem() {
        return select;
    }

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.dialog_list, this);

        dialog_content = (TextView) this.findViewById(R.id.tv_dialog_content);
        lin_dialog = (LinearLayout) this.findViewById(R.id.lin_dialog);
        lin_dialog.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag)
                    alertDialog.show();
            }
        });

        alertBuilder = new AlertDialog.Builder(context);
    }

    private void setTitle() {
        dialog_content.setText(title);
        dialog_content.setTextColor(context.getResources().getColor(R.color.text_black_202020));
    }

    private void setItems() {
        alertBuilder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialoginterface, int index) {
                dialog_content.setText(items[index]);
                dialog_content.setTextColor(context.getResources().getColor(R.color.text_black_202020));
                alertDialog.dismiss();
                changeIndex(index);
            }
        });
        alertDialog = alertBuilder.create();
    }

    private void initData(List<BaseEntity> baseEntities) {
        setTitle();

        if (null == baseEntities) {
            baseEntities = new ArrayList<BaseEntity>();
        }

        items = new String[baseEntities.size()];
        for (int i = 0; i < baseEntities.size(); i++) {
            items[i] = baseEntities.get(i).getName();
        }
    }

    private void changeIndex(int index) {
        if (select != index) {
            select = index;

            if (null != listDialogCallBackListener) {
                listDialogCallBackListener.OnChange(index);
            }
        }
    }
}